package com.stone.design.practice.mvp.presenter;

import android.os.Handler;
import android.os.Looper;

import com.stone.design.practice.mvp.model.IUser;
import com.stone.design.practice.mvp.model.UserModel;
import com.stone.design.practice.mvp.view.ILoginView;



/**
 * @author Stone
 * @version V1.0
 * @description:
 * @date 7/6/18 09:15
 * @email redsstone@163.com
 */
public class LonginPresenterCompl implements ILoginPresenter{

    private IUser mIUser;
    private ILoginView mLoginView;

    Handler mHandler;

    public LonginPresenterCompl(ILoginView loginView) {
        mLoginView = loginView;
        initUser();
        mHandler = new Handler(Looper.getMainLooper());
    }

    @Override
    public void clear() {
        mLoginView.onClearText();
    }

    @Override
    public void doLogin(String name, String password) {

        Boolean isLoginSuccess = true;
        final int code = mIUser.checkUserValidity(name,password);
        if (code!=0) isLoginSuccess = false;
        final Boolean result = isLoginSuccess;
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mLoginView.onLoginResult(result, code);
            }
        }, 5000);
    }

    @Override
    public void setProgressBarVisibility(int visibility) {

        mLoginView.onSetProgressBarVisibility(visibility);
    }

    private void initUser() {
        mIUser = new UserModel("stone", "hah");
    }
}
